package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_DAMAGE_RANK_LIST, desc = "跨服获取怪物伤害榜单")
public class CrossGhostGetDamageRankListCmd extends CrossCommand {

    int activityId;
    int bossId;
    long serverId;
    long playerId;

    RankProto.RankListMsg.Builder rankListMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossGetGhostDamageRankReqMsg reqMsg = GhostActivityProto.CrossGetGhostDamageRankReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        bossId = reqMsg.getBossId();
        serverId = reqMsg.getServerId();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        rankListMsg = activity.getRankListModel(bossId).getRankListMsg();
    }

    @Override
    public void sendData() {
        GhostActivityProto.GetGhostDamageRankRespMsg.Builder builder = GhostActivityProto.GetGhostDamageRankRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setRankList(rankListMsg);
        }
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(ClientProtocol.U_GHOST_DAMAGE_RANK_LIST, builder));
    }
}
